/** @file    SecurityAuthenticationOS.cpp
 *  @author  Alessandro Polo
 *  @version $Id: SecurityAuthenticationOS.cpp 2832 2010-08-02 22:53:46Z alex $
 *  @brief
 * File containing methods for the wosh::security::SecurityAuthenticationOS class.
 * The header for this class can be found in SecurityAuthenticationOS.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <framework/security/SecurityAuthenticationOS.h>

 #include <core/User.h>
 #include <core/UserManager.h>
 #include <core/ObjectAllocator.h>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _OS_POSIX
# include <core/posix/desktop.h>
#elif _OS_WIN32
# include <core/win32/desktop.h>
#elif _OS_WINCE
# include <core/wince/desktop.h>
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


using namespace std;
namespace wosh {
 namespace security {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::security::SecurityTokenOS, "wosh::SecurityToken", _SecurityTokenOS_VERSION, _static_SecurityTokenOS)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::security::SecurityTokenProviderOS, "wosh::SecurityTokenProvider", _SecurityTokenOS_VERSION, _static_SecurityTokenProviderOS)

SecurityTokenProviderOS::SecurityTokenProviderOS()
	: SecurityTokenProvider() {
	getOSName(this->data.os_name);
	getComputerName(this->data.os_hostname);
 }

SecurityTokenProviderOS::SecurityTokenProviderOS( const SecurityTokenProviderOS& m )
	: SecurityTokenProvider(m) {
 }

void SecurityTokenProviderOS::setHostName( const std::string& host_name ) {
	this->data.os_hostname = host_name;
 }

void SecurityTokenProviderOS::setOsType( SecurityTokenProviderOS_LoginType l_type ) {
	this->data.type = l_type;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT SecurityTokenProviderOS::findLoggedUser( std::string& wosh_user, std::string* os_user ) {
	string tempUser;
	bool userOk = getSystemUserName(tempUser);
	if ( !userOk )
		return WRET_ERR_INTERNAL;
	string tempHostname;
	getComputerName(tempHostname);
	if ( os_user != NULL )
		*os_user = tempUser;
	///@bug WIN_Username key MUST be dynamic!!!
	std::vector<std::string> users;
	if ( tempHostname.empty() )
		users = UserManager::findUserByProperty( "OsUsername_*", Variant(tempUser) );
	else
		users = UserManager::findUserByProperty( "OsUsername_" + tempHostname, Variant(tempUser) );
	if ( users.size() == 0 )
		return WRET_ERR_PARAM;
	wosh_user = users[0];
	return WRET_OK;
 }

WRESULT SecurityTokenProviderOS::gatherOsHostName( std::string& os_hostname ) {
	bool nameOk = getComputerName(os_hostname);
	if ( !nameOk ) return WRET_ERR_INTERNAL;
	return WRET_OK;
 }

WRESULT SecurityTokenProviderOS::gatherOsName( std::string& os_name ) {
	bool nameOk = getOSName(os_name);
	if ( !nameOk ) return WRET_ERR_INTERNAL;
	return WRET_ERR_INTERNAL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

SecurityToken* SecurityTokenProviderOS::createToken() const {
	SecurityTokenOS* st = new SecurityTokenOS();
	getComputerName(this->data.os_hostname);
	st->data = this->data;
	return st;
 }

SecurityTokenOS* SecurityTokenProviderOS::createToken( const std::string& os_account_name ) const {
	SecurityTokenOS* st = new SecurityTokenOS();
	getComputerName(this->data.os_hostname);
	st->data = this->data;
	st->os_username = os_account_name;
	return st;
 }

SecurityToken* SecurityTokenProviderOS::signMessage( Message* message, const std::string& username, const std::string& os_account_name ) const {
	SecurityTokenOS* st = createToken(os_account_name);
	initToken(st);
	updateToken(st, username);
	finalizeSigning(st, message);
	return st;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::security::SecurityTokenAuthenticatorOS, "wosh::SecurityTokenAuthenticator", _SecurityTokenOS_VERSION, _static_SecurityTokenAuthenticatorOS)

SecurityTokenAuthenticatorOS::SecurityTokenAuthenticatorOS()
	: SecurityTokenAuthenticator() {
 }
//SecurityTokenAuthenticatorOS::SecurityTokenAuthenticatorOS( const SecurityTokenAuthenticatorOS& m )
//	: SecurityTokenAuthenticator(m) {
// }

bool SecurityTokenAuthenticatorOS::isSupported( const char* provider_classname, double version ) const {
	if ( provider_classname == NULL ) return false;
	if ( strcmp(provider_classname, SecurityTokenOS::className()) != 0 ) return false;
	if ( version != 0.0 && version > _SecurityTokenOS_VERSION ) return false;
	return true;
 }

WRESULT SecurityTokenAuthenticatorOS::validateToken( const SecurityToken* token ) const {
	return WRET_ERR_INTERNAL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


 


















































/*

 WOSH_REGISTER(wosh::security::SecurityAuthenticationOS, "wosh::SecurityAuthentication", 1.0, _static_SecurityAuthenticationOS )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

SecurityAuthenticationOS::SecurityAuthenticationOS()
	: SecurityAuthentication() {
 }

SecurityAuthenticationOS::SecurityAuthenticationOS( const SecurityAuthenticationOS& m )
	: SecurityAuthentication(m) {
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT SecurityAuthenticationOS::validate( const wosh::User* user ) const {
	if ( user == NULL ) return WRET_ERR_PARAM;

	string winUser = "";
	bool userOk = getSystemUserName(winUser);
	if ( !userOk )
		return WRET_ERR_INTERNAL;

	Variant mapped_win_user = user->getProperties().getValueOf("WIN_Username" );
	if ( !mapped_win_user.isStringNotEmpty() || mapped_win_user.asString() != winUser )
		return WRET_ERR_PARAM;
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT SecurityAuthenticationOS::findLoggedUser( std::string& wosh_user, std::string* os_user ) {
	string tempUser;
	bool userOk = getSystemUserName(tempUser);
	if ( !userOk )
		return WRET_ERR_INTERNAL;
	if ( os_user != NULL )
		*os_user = tempUser;
	std::vector<std::string> users = UserManager::findUserByProperty( "WIN_Username", Variant(tempUser) );
	if ( users.size() == 0 )
		return WRET_ERR_PARAM;
	wosh_user = users[0];
	return WRET_OK;
 }
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace security
}; // namespace wosh
